#include "database/Client.hpp"
#include "Protocol.h"
#include "TCPServer.h"

void CSProtocol::AddContact::receiveTreat(TCPConnection &connexion, IData *udata)
{
  CSProtocol::AddContact *data = static_cast<CSProtocol::AddContact*>(udata);
  if (!data || !connexion.getAuthenticate())
    return;
  CSProtocol::Header header = CSProtocol::Header();
  header.spontaneous = false;
  IData *pcmd;
  if (((connexion.getServer())->getDB()).clientExist(data->login))
    {
      std::string slogin = std::string(data->login);
      std::string scategory = std::string(data->category);
      ClientCategory *cc = new ClientCategory(slogin, scategory);
      connexion.getClient()->addFriends(cc);
      header.code = CSProtocol::Ok::code;
      CSProtocol::Ok command = CSProtocol::Ok();
      pcmd = &command;
      strcpy(command.what, "New Friends Ok..");
      header.dataLength = sizeof(CSProtocol::Ok);
    }
  else
    {
      header.code = CSProtocol::Ko::code;
      CSProtocol::Ko command = CSProtocol::Ko();
      strcpy(command.what, "Friends doesn't exist.");
      pcmd = &command;
      header.dataLength = sizeof(CSProtocol::Ko);
    }
  connexion.write(header, *pcmd);
}

void CSProtocol::AddContact::sendTreat(TCPConnection &connexion)
{

}
